package com.ls.lscrm.web.controller;

import com.ls.lscrm.web.model.CRUDResult;
import com.ls.lscrm.web.model.CourseOrder;
import com.ls.lscrm.web.model.PageResult;
import com.ls.lscrm.web.service.ICourseOrderService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/courseorder")
public class CourseOrderController {
    @Resource
    ICourseOrderService service;

    @RequestMapping("/list")
    public String list() {
        return "courseorder/list";
    }

    @RequestMapping("/add")
    public String add() {
        return "courseorder/add";
    }

    /**
     * @param page  当前页
     * @param limit 每次显示多少条
     * @return
     */
    @RequestMapping("/listjson")
    @ResponseBody
    public PageResult<CourseOrder> listJson(CourseOrder codition, int page, int limit) {
        PageResult<CourseOrder> result = service.find(codition, page, limit);
        return result;
    }

    @RequestMapping("/save")
    @ResponseBody
    public CRUDResult save(CourseOrder order) {

        CRUDResult result = new CRUDResult();
        if (order.getOrder_id() == null || order.getOrder_id().length() == 0) {
            //添加操作
            result = service.save(order);
        } else {
            result = service.update(order);
        }

        return result;
    }

    @RequestMapping("/detail")
    public String detail(Model model, String order_id) {
        CourseOrder order = service.findByOrderId(order_id);
        model.addAttribute("order", order);
        return "courseorder/detail";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public CRUDResult delete(String order_id) {
        CRUDResult result = service.delete(order_id);
        return result;
    }

    @RequestMapping("/edit")
    //编辑，弹出编辑页面
    public String edit(Model model, String order_id) {
        CourseOrder order = service.findByOrderId(order_id);
        model.addAttribute("order", order);
        return "courseorder/edit";
    }
}
